package freeipa

import (
	"fmt"
	"youdu/config"
	"youdu/service/ipa"
	"youdu/service/yodoo"

	"github.com/gin-gonic/gin"
)

func UserAuth(c *gin.Context) {
	var json struct {
		Sign    string `json:"msg_signature"`
		Encrpty string `json:"encrypt"`
	}
	if err := c.BindJSON(&json); err != nil {
		c.JSON(200, yodoo.NewResponse(500, "Json err: "+err.Error(), ""))
		fmt.Println("回调参数获取失败：" + err.Error())
		return
	}

	username, password, err := yodoo.ThirdAuthDataDecode(config.Inst.YouduApp.SysOrg.AesKey, json.Encrpty)
	if err != nil {
		c.JSON(200, yodoo.NewResponse(500, "Decode err: "+err.Error(), ""))
		fmt.Println("密文解密失败：" + err.Error() + "，KEY：" + config.Inst.YouduApp.SysOrg.AesKey)
		return
	}

	err = ipa.LoginFreeIPA(username, password)
	if err == nil {
		yodoo.LoginSuccess(c)
	} else {
		yodoo.LoginFail(c)
		fmt.Println(username + ":" + password)
		fmt.Println("登录验证失败：" + err.Error())
	}
	// c.JSON(200, yodoo.NewResponse(200, "Json err: "+json.Sign, ""))
}
